﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;
namespace Ecreate.Web.systemanage.customer
{
    public partial class customercrud : System.Web.UI.Page
    {
        protected string areaname = "";
        Ecreate.Popedom.Service.SpringImpl.UseStateImpl usestate = new Ecreate.Popedom.Service.SpringImpl.UseStateImpl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                string areacode = this.Request.QueryString["areacode"];
                string action = this.Request.QueryString["action"];
                string provincecode = "";
                string citycode = "";
                if (areacode != null&&areacode!="")
                {
                    hid_areacode.Value = areacode;
                    this.action.Value = action;
                    provincecode = areacode.Substring(0, 2);
                    citycode = areacode.Substring(0, 4);
                }
                utility.SessionObject session = utility.SessionManage.getSession(this);
                if ("insert".Equals(action))
                {
                    if (session.Admin.Group.Grouplevel.Flevel == 0)
                    {
                        //HtmlHelper.ShowMessage(this, "管理员不允许添加销售商!");
                        //this.Response.Redirect(this.Request.UrlReferrer.AbsoluteUri);
                    }
                    if (session.Admin.Group.Grouplevel.Flevel == 3)
                    {
                        HtmlHelper.ShowMessage(this, "宣传员不允许添加销售商!");
                        this.Response.Redirect(this.Request.UrlReferrer.AbsoluteUri);
                    }
                }



                //this.setDropDownList(provincecode, citycode, areacode);


                if (session.Admin.Group.Grouplevel.Flevel == 0)
                {

                    AreaService areaservice = new AreaService();
                    ddl_province.DataSource = areaservice.getProvinceArea();
                    ddl_province.DataValueField = "areacode";
                    ddl_province.DataTextField = "areaname";
                    ddl_province.DataBind();

                    this.ddl_city.DataSource = areaservice.getCityArea(ddl_province.SelectedValue);
                    this.ddl_city.DataValueField = "areacode";
                    this.ddl_city.DataTextField = "areaname";
                    this.ddl_city.DataBind();

                    this.ddl_country.DataSource = areaservice.getCountryArea(this.ddl_city.SelectedValue);
                    this.ddl_country.DataValueField = "areacode";
                    this.ddl_country.DataTextField = "areaname";
                    this.ddl_country.DataBind();

                }


                this.ddl_usestate.DataSource = usestate.getAllState();
                this.ddl_usestate.DataTextField = "Name";
                this.ddl_usestate.DataValueField = "Id";
                this.ddl_usestate.DataBind();

                if ("edit".Equals(action))
                {
                    string cus_id = this.Request.QueryString["id"];
                    this.cus_id.Value = cus_id;
                    CustomerService service = new CustomerService();
                    t_customer model = service.getCustomerByID(cus_id);
                    if (model != null)
                    {
                        this.txt_cus_id.ReadOnly = true;
                        this.txt_cus_id.Text = model.Cus_id;
                        this.txt_cus_name.Text = model.Cus_name;
                        this.txt_cus_address.Text = model.Cus_address;
                        this.txt_cus_linkman.Text = model.Cus_linkman;
                        this.txt_cus_tel.Text = model.Cus_tel;
                        this.cus_id.Value = model.Cus_id;
                        this.txt_password.Text = model.Password;
                        this.txt_account.Text = model.Cus_account;
                        areacode = model.Areacode;
                        provincecode = model.Areacode.Substring(0, 2);
                        citycode = model.Areacode.Substring(0, 4);
                        this.setDropDownList(provincecode, citycode, areacode);
                        this.hid_areacode.Value = areacode;
                        this.ddl_usestate.SelectedValue = model.Useflag.ToString();
                    }
                }
                else if ("delete".Equals(action))
                {

                    string cus_id = this.Request.QueryString["id"];
                    CustomerService service = new CustomerService();
                    t_customer model = service.getCustomerByID(cus_id);
                    service.Delete(model);
                    this.Response.Redirect(this.Request.UrlReferrer.AbsoluteUri);
                }



                if (session.Admin.Group.Grouplevel.Flevel == 1)
                {
                    AreaService areaservice = new AreaService();
                    ddl_province.DataSource = areaservice.getProvinceArea();
                    ddl_province.DataValueField = "areacode";
                    ddl_province.DataTextField = "areaname";
                    ddl_province.DataBind();
                    ddl_province.SelectedValue = session.Admin.Province.Id;
                    ddl_province.Enabled = false;
                    this.ddl_city.DataSource = areaservice.getCityArea(ddl_province.SelectedValue);
                    this.ddl_city.DataValueField = "areacode";
                    this.ddl_city.DataTextField = "areaname";
                    this.ddl_city.DataBind();

                    this.ddl_country.DataSource = areaservice.getCountryArea(this.ddl_city.SelectedValue);
                    this.ddl_country.DataValueField = "areacode";
                    this.ddl_country.DataTextField = "areaname";
                    this.ddl_country.DataBind();


                }
                if (session.Admin.Group.Grouplevel.Flevel == 2)
                {
                    AreaService areaservice = new AreaService();
                    ddl_province.DataSource = areaservice.getProvinceArea();
                    ddl_province.DataValueField = "areacode";
                    ddl_province.DataTextField = "areaname";
                    ddl_province.DataBind();
                    ddl_province.SelectedValue = session.Admin.Province.Id;
                    ddl_province.Enabled = false;
                    this.ddl_city.DataSource = areaservice.getCityArea(ddl_province.SelectedValue);
                    this.ddl_city.DataValueField = "areacode";
                    this.ddl_city.DataTextField = "areaname";
                    this.ddl_city.DataBind();
                    ddl_city.SelectedValue = session.Admin.City.Id;
                    ddl_city.Enabled = false;

                    this.ddl_country.DataSource = areaservice.getCountryArea(this.ddl_city.SelectedValue);
                    this.ddl_country.DataValueField = "areacode";
                    this.ddl_country.DataTextField = "areaname";
                    this.ddl_country.DataBind();

                }

            }
        }

        public void setDropDownList(string provincecode,string citycode,string countrycode)
        {



            AreaService aservice = new AreaService();

            if (provincecode == "")
            {
                this.ddl_province.DataSource = aservice.getProvinceArea();
            }
            else
            {
                this.ddl_province.DataSource = aservice.getProvinceArea(provincecode);
            }
            this.ddl_province.DataValueField = "areacode";
            this.ddl_province.DataTextField = "areaname";
            this.ddl_province.DataBind();

            if (ddl_province.SelectedValue != null)
            {
                string id = ddl_province.SelectedValue.ToString();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();
                if (citycode != "")
                {
                    ddl_city.SelectedValue = citycode;
                }


            }
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
                if (countrycode != "")
                {

                    this.ddl_country.SelectedValue = countrycode;
                }
            }

        }




        protected void ddl_province_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_province.SelectedValue!=null)
            {
                string id = ddl_province.SelectedValue.ToString();
                AreaService aservice = new AreaService();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();


                ddl_city_SelectedIndexChanged(null, null);
            }
        }

        protected void ddl_city_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                AreaService aservice = new AreaService();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.ddl_province.SelectedIndex == -1)
            {
                this.lbl_message.Text = "请选择省份";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.ddl_city.SelectedIndex == -1)
            {
                this.lbl_message.Text = "请选择城市";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.ddl_country.SelectedIndex == -1)
            {
                this.lbl_message.Text = "请选择县";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.txt_cus_id.Text == "")
            {
                this.lbl_message.Text = "请输入用户ID";
                this.lbl_message.Visible = true;
                return;
            }

            if (this.txt_cus_name.Text == "")
            {
                this.lbl_message.Text = "请输入用户名称";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.txt_cus_address.Text == "")
            {
                this.lbl_message.Text = "请输入销售商地址";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.txt_cus_tel.Text == "")
            {
                this.lbl_message.Text = "请输入销售商电话";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.txt_cus_linkman.Text == "")
            {
                this.lbl_message.Text = "请输入联系人";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.ddl_usestate.SelectedIndex == -1)
            {
                this.lbl_message.Text = "请选择用户状态";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.txt_password.Text.Trim() == "")
            {
                this.lbl_message.Text = "密码不允许为空!";
                this.lbl_message.Visible = true;
                return;
            }
            if (this.txt_account.Text.Trim() == "")
            {
                this.lbl_message.Text = "账号不允许为空!";
                this.lbl_message.Visible = true;
                return;
            }





            if (this.action.Value == "edit")
            {
                CustomerService service = new CustomerService();
                t_customer cus = new t_customer();
                cus.Cus_id = this.cus_id.Value;
                cus.Cus_name = this.Request.Form["txt_cus_name"];
                cus.Cus_address = this.Request.Form["txt_cus_address"];
                cus.Cus_linkman = this.Request.Form["txt_cus_linkman"];
                cus.Cus_tel = this.Request.Form["txt_cus_tel"];
                cus.Areacode = this.ddl_country.SelectedValue.ToString();
                cus.Useflag = int.Parse(this.ddl_usestate.SelectedValue);
                cus.Password = this.txt_password.Text;
                cus.Cus_account = this.txt_account.Text;
                utility.SessionObject session = utility.SessionManage.getSession(this);
                customerflagmanage.setCancelDate(cus);
                service.Update(cus);
            }
            else
            {
                CustomerService service = new CustomerService();
                t_customer cus = new t_customer();
                cus.Cus_id = this.txt_cus_id.Text;
                cus.Cus_name = this.Request.Form["txt_cus_name"];
                cus.Cus_address = this.Request.Form["txt_cus_address"];
                cus.Cus_linkman = this.Request.Form["txt_cus_linkman"];
                cus.Cus_tel = this.Request.Form["txt_cus_tel"];
                cus.Areacode = this.ddl_country.SelectedValue.ToString();
                cus.Useflag = int.Parse(this.ddl_usestate.SelectedValue);
                cus.Password = this.txt_password.Text;
                cus.Cus_account = this.txt_account.Text;
                utility.SessionObject session = utility.SessionManage.getSession(this);

                if (session.Admin.Group.Grouplevel != null)
                {
                    if (session.Admin.Group.Grouplevel.Flevel == 1)
                    {
                        cus.Level_one_userid = session.Admin.Id;
                        cus.Level_two_userid = "";
                    }
                    else if (session.Admin.Group.Grouplevel.Flevel == 2)
                    {

                        cus.Level_one_userid = "";
                        cus.Level_two_userid = session.Admin.Id;
                    }

                }
                customerflagmanage.setCancelDate(cus);

                WebResult<t_customer> web=service.Insert(cus);
                if (web.Success == false)
                {
                    //Response.Write("<script type=\"text/javascript\" language=\"javascript\">          alert('数据保存成功!')　　; </script>");
                    HtmlHelper.ShowMessage(this, web.Message);
                    return;
                }
            }
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">          alert('数据保存成功!')　　; </script>");
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"contentIframe\"].location=\"/systemanage/customer/customerlist.aspx?areacode=" + this.hid_areacode.Value + "\"　　; </script>");
        }
    }
}
